package com.vipshop.compass.dao;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.vipshop.compass.common.GenericDaoHibernate;
import com.vipshop.compass.domain.User;

@Repository("userDao")
public class UserDao extends GenericDaoHibernate<User, Integer> {

	public UserDao(Class<User> persistentClass) {
		super(persistentClass);
	}

	public UserDao() {
		super(User.class);
	}

	public User findByName(String name) {
		Query qry = getSession().createQuery("from User u where u.name=:name");
		qry.setParameter("name", name);
		return (User) qry.uniqueResult();
	}

	public User findByLoginName(String loginName) {
		Query qry = getSession().createQuery("from User u where u.loginName=:loginName");
		qry.setParameter("loginName", loginName);
		return (User) qry.uniqueResult();
	}

	public boolean existsByName(String name) {
		Query qry = getSession().createQuery("select count(*) from User u where u.name=:name");
		qry.setParameter("name", name);
		return ((Long) qry.uniqueResult()).intValue() > 0;
	}
	
	public boolean existsByLoginName(String loginName) {
		Query qry = getSession().createQuery("select count(*) from User u where u.loginName=:loginName");
		qry.setParameter("loginName", loginName);
		return ((Long) qry.uniqueResult()).intValue() > 0;
	}

}
